package cn.dengta.webapp.user.entity;

import java.util.Date;
import javax.annotation.Nullable;

import cn.dengta.common.context.I18N;
import cn.dengta.common.model.State;
import cn.dengta.common.model.Status;
import cn.dengta.context.model.Language;
import cn.dengta.context.model.UserI18nKey;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.user.model.PlanetExploreType;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.Assert;
import me.codeplayer.util.X;

/**
 * 星球探险日志
 *
 * @author lgy
 * @since 2022-10-13
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("act_planet_explore_log")
public class ActPlanetExploreLog extends BizEntity {

	/** 用户ID */
	Long userId;
	/** 星球探险ID */
	Long actPlanetExploreId;
	/** 金星 */
	Integer planet;
	/** 金星押注数 */
	Integer betCount;
	/** 金星钻石数 */
	Integer coin;
	/** 木星 */
	Integer planet1;
	/** 木星押注数 */
	Integer betCount1;
	/** 木星钻石数 */
	Integer coin1;
	/** 水星 */
	Integer planet2;
	/** 水星押注数 */
	Integer betCount2;
	/** 水星钻石数 */
	Integer coin2;
	/** 火星 */
	Integer planet3;
	/** 火星押注数 */
	Integer betCount3;
	/** 火星钻石数 */
	Integer coin3;
	/** 土星 */
	Integer planet4;
	/** 土星押注数 */
	Integer betCount4;
	/** 土星钻石数 */
	Integer coin4;
	/** 天王星 */
	Integer planet5;
	/** 天王星押注数 */
	Integer betCount5;
	/** 天王星钻石数 */
	Integer coin5;
	/** 海王星 */
	Integer planet6;
	/** 海王押注数 */
	Integer betCount6;
	/** 海王钻石数 */
	Integer coin6;
	/** 开普勒星 */
	Integer planet7;
	/** 开普勒星押注数 */
	Integer betCount7;
	/** 开普勒星钻石数 */
	Integer coin7;
	/** 押注总钻石 */
	Integer totalCoin;
	/** 中奖钻石数 */
	Integer prizeCoin;
	/** 中奖星球 */
	Integer prizePlanet;

	public ActPlanetExploreLog init(Long userId, Long actPlanetExploreId, PlanetExploreType planet, Integer betCount, Integer coin, Integer maxLimit, Date now) {
		this.userId = userId;
		this.actPlanetExploreId = actPlanetExploreId;
		this.planet = PlanetExploreType.VENUS.value;
		this.betCount = this.coin = 0;
		this.planet1 = PlanetExploreType.JUPITER.value;
		this.betCount1 = this.coin1 = 0;
		this.planet2 = PlanetExploreType.MERCURY.value;
		this.betCount2 = this.coin2 = 0;
		this.planet3 = PlanetExploreType.MARS.value;
		this.betCount3 = this.coin3 = 0;
		this.planet4 = PlanetExploreType.SATURN.value;
		this.betCount4 = this.coin4 = 0;
		this.planet5 = PlanetExploreType.URANUS.value;
		this.betCount5 = this.coin5 = 0;
		this.planet6 = PlanetExploreType.NEPTUNE.value;
		this.betCount6 = this.coin6 = 0;
		this.planet7 = PlanetExploreType.KEPLER.value;
		this.betCount7 = this.coin7 = 0;
		this.totalCoin = this.prizeCoin = 0;
		this.prizePlanet = null;
		addBetCount(planet, betCount, coin, maxLimit, now);
		super.init(Status.NO.value, State.PUBLIC);
		return this;
	}

	public static String[] planetType(int type) {
		String planet = "planet";
		String betCount = "bet_count";
		String coin = "coin";
		if (type > 0) {
			planet = planet + type;
			betCount = betCount + type;
			coin = coin + type;
		}
		return new String[] { planet, betCount, coin };
	}

	public Integer calcBetCount() {
		return betCount + betCount1 + betCount2 + betCount3 + betCount4 + betCount5 + betCount6 + betCount7;
	}

	public Integer calcCoin() {
		return coin + coin1 + coin2 + coin3 + coin4 + coin5 + coin6 + coin7;
	}

	public Integer calcPrizeBetCount(Integer planet) {
		PlanetExploreType type = PlanetExploreType.of(X.expectNotNull(planet, prizePlanet));
		int count = 0;
		if (type != null) {
			switch (type) {
				case VENUS:
					count = betCount;
					break;
				case JUPITER:
					count = betCount1;
					break;
				case MERCURY:
					count = betCount2;
					break;
				case MARS:
					count = betCount3;
					break;
				case SATURN:
					count = betCount4;
					break;
				case URANUS:
					count = betCount5;
					break;
				case NEPTUNE:
					count = betCount6;
					break;
				case KEPLER:
					count = betCount7;
					break;
			}
		}
		return count;
	}

	public ActPlanetExploreLog addBetCount(PlanetExploreType planet, Integer betCount, Integer coin, Integer maxLimit, Date now) {
		int count = 0;
		switch (planet) {
			case VENUS:
				this.betCount += betCount;
				this.coin += coin;
				count = this.betCount;
				break;
			case JUPITER:
				this.betCount1 += betCount;
				this.coin1 += coin;
				count = this.betCount1;
				break;
			case MERCURY:
				this.betCount2 += betCount;
				this.coin2 += coin;
				count = this.betCount2;
				break;
			case MARS:
				this.betCount3 += betCount;
				this.coin3 += coin;
				count = this.betCount3;
				break;
			case SATURN:
				this.betCount4 += betCount;
				this.coin4 += coin;
				count = this.betCount4;
				break;
			case URANUS:
				this.betCount5 += betCount;
				this.coin5 += coin;
				count = this.betCount5;
				break;
			case NEPTUNE:
				this.betCount6 += betCount;
				this.coin6 += coin;
				count = this.betCount6;
				break;
			case KEPLER:
				this.betCount7 += betCount;
				this.coin7 += coin;
				count = this.betCount7;
				break;
		}
		Assert.isTrue(maxLimit == null || count <= maxLimit, () -> I18N.msg(UserI18nKey.AIRSHIP_MAX_LIMIT, maxLimit));
		totalCoin += coin;
		super.initTime(now);
		return this;
	}

	public static String planetParam(int type) {
		return planetType(type)[0];
	}

	public static String betCountParam(int type) {
		return planetType(type)[1];
	}

	public static String coinParam(int type) {
		return planetType(type)[2];
	}

	public String wrapName(@Nullable Language language) {
		if (language == null) {
			language = Language.en;
		}
		StringBuilder sb = new StringBuilder(128);
		appendName(sb, betCount, planet, betCount1, planet1, betCount2, planet2, betCount3, planet3, language);
		appendName(sb, betCount4, planet4, betCount5, planet5, betCount6, planet6, betCount7, planet7, language);
		return sb.toString();
	}

	private void appendName(StringBuilder sb, Integer betCount4, Integer planet4, Integer betCount5, Integer planet5, Integer betCount6, Integer planet6, Integer betCount7, Integer planet7, Language language) {
		if (betCount4 > 0) {
			appendName(sb, planet4, betCount4, language);
		}
		if (betCount5 > 0) {
			appendName(sb, planet5, betCount5, language);
		}
		if (betCount6 > 0) {
			appendName(sb, planet6, betCount6, language);
		}
		if (betCount7 > 0) {
			appendName(sb, planet7, betCount7, language);
		}
	}

	private void appendName(StringBuilder sb, Integer planet, Integer count, Language language) {
		sb.append(PlanetExploreType.of(planet).getLabel(language)).append("x").append(count).append("  ");
	}

	public static final String USER_ID = "user_id";
	public static final String ACT_PLANET_EXPLORE_ID = "act_planet_explore_id";
	public static final String PLANET = "planet";
	public static final String BET_COUNT = "bet_count";
	public static final String COIN = "coin";
	public static final String PLANET1 = "planet1";
	public static final String BET_COUNT1 = "bet_count1";
	public static final String COIN1 = "coin1";
	public static final String PLANET2 = "planet2";
	public static final String BET_COUNT2 = "bet_count2";
	public static final String COIN2 = "coin2";
	public static final String PLANET3 = "planet3";
	public static final String BET_COUNT3 = "bet_count3";
	public static final String COIN3 = "coin3";
	public static final String PLANET4 = "planet4";
	public static final String BET_COUNT4 = "bet_count4";
	public static final String COIN4 = "coin4";
	public static final String PLANET5 = "planet5";
	public static final String BET_COUNT5 = "bet_count5";
	public static final String COIN5 = "coin5";
	public static final String PLANET6 = "planet6";
	public static final String BET_COUNT6 = "bet_count6";
	public static final String COIN6 = "coin6";
	public static final String PLANET7 = "planet7";
	public static final String BET_COUNT7 = "bet_count7";
	public static final String COIN7 = "coin7";
	public static final String TOTAL_COIN = "total_coin";
	public static final String PRIZE_COIN = "prize_coin";
	public static final String PRIZE_PLANET = "prize_planet";

}
